<?php
namespace Modules\Weixin\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Services\WechatService;
use Illuminate\Http\Request;
use Modules\Weixin\Entities\WxMenu;
use Modules\Weixin\Http\Requests\WxMenuRequest;
class WxMenuController extends Controller
{
    //显示列表
    public function index()
    {
        $data = WxMenu::paginate(10);
        return view('weixin::wx_menu.index', compact('data'));
    }

    //创建视图
    public function create(WxMenu $wx_menu)
    {
        return view('weixin::wx_menu.create',compact('wx_menu'));
    }

    //保存数据
    public function store(WxMenuRequest $request,WxMenu $wx_menu)
    {
    	$post=$request->all();

    	$post['data']=json_decode ($post['data'],true);
		//dd($post['data']);
        $wx_menu->fill($post);
		$wx_menu->save();
        flash('添加菜单成功！')->overlay ();


        return redirect('/weixin/wx_menu');
    }

    //显示记录
    public function show(WxMenu $field)
    {
        return view('weixin::wx_menu.show', compact('field'));
    }

    //编辑视图
    public function edit(WxMenu $wx_menu)
    {

        return view('weixin::wx_menu.edit', compact('wx_menu'));
    }

    //更新数据
    public function update(WxMenuRequest $request, WxMenu $wx_menu)
    {
    	$post=$request->all();
    	$post['data']=json_decode ($post['data'],true);
        $wx_menu->update($post);
        return redirect('/weixin/wx_menu')->with('success','更新成功');
    }

    //删除模型
    public function destroy(WxMenu $wx_menu)
    {
        $wx_menu->delete();
        return redirect('weixin/wx_menu')->with('success','删除成功');
    }
	//推送菜单到微信端
	public function push(WxMenu $wx_menu,WechatService $wechatService){
		$res = $wechatService->instance('button')->create(['button' => $wx_menu['data']]);
		//        $res = WeChat::instance('button')->create(['button' => $wx_menu['data']]);
		if ($res['errcode'] == 0){
			return back()->with('success','自定义菜单推送到微信成功,请打开手机微信公众号查看');
		}
		return back()->with('danger',$res['errmsg']);
	}
}
